
using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using OutlookBarWorkspaceQuickstart.MailModule.Constants;
using System.Drawing;
using SCSFContrib.CompositeUI.WinForms.Workspaces;

namespace OutlookBarWorkspaceQuickstart.MailModule
{
    public class MailFoldersPresenter : IDisposable
    {
        private IMailFolders _view = null;
        private WorkItem _workItem = null;

        ~MailFoldersPresenter()
        {
            Dispose(false);
        }

        /// <summary>
        /// See <see cref="System.IDisposable.Dispose"/> for more information.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Called when the object is being disposed or finalized.
        /// </summary>
        /// <param name="disposing">True when the object is being disposed (and therefore can
        /// access managed members); false when the object is being finalized without first
        /// having been disposed (and therefore can only touch unmanaged members).</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_workItem != null)
                    _workItem.Items.Remove(this);
            }
        }

        /// <summary>
        /// Sets the view.
        /// </summary>
        public IMailFolders View
        {
            set { _view = value; }
        }

        /// <summary>
        /// The WorkItem; provided during dependency injection. Used to automatically
        /// remove the presenter from the WorkItem when it's being disposed.
        /// </summary>
        [ServiceDependency]
        public WorkItem WorkItem
        {
            set { _workItem = value; }
        }

        internal Microsoft.Practices.CompositeUI.SmartParts.ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
        {
            ISmartPartInfo spi = null;

            if (smartPartInfoType.IsAssignableFrom(typeof(OutlookBarSmartPartInfo)))
            {
                OutlookBarSmartPartInfo ospi = new OutlookBarSmartPartInfo();
                ospi.Icon = Properties.Resources.Mail24;
                ospi.EventTopicName = EventTopicNames.ShowInbox;
                ospi.ImageTransparentColor = Color.FromArgb(238, 238, 238);
                ospi.Index = 1;
                spi = ospi;
            }
            else
            {
                spi = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
            }

            spi.Description = Properties.Resources.MailBarDescription;
            spi.Title = Properties.Resources.MailBarTitle;

            return spi; 
                       
        }
    }
}

